﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using DosyaUpload;

namespace LocumUpdate
{
    public delegate void FtpDownloadUpdate(int downloaded, int totalDownload);
    public class Baglanti
    {

        public event FtpDownloadUpdate ftpDownloadUpdate;


        private string userName;
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string userPassword;
        public string UserPassword
        {
            get { return userPassword; }
            set { userPassword = value; }
        }

        private string siteAdresi;
        public string SiteAdresi
        {
            get { return siteAdresi; }
            set { siteAdresi = value; }
        }
        

        public string getVersion(string versionConfigPath)
        {
            StreamReader sr = new StreamReader(versionConfigPath);

            string versionConfg = sr.ReadToEnd();
            string key = "ProgramVersiyon";
            string textstr = string.Empty;
            int x = versionConfg.IndexOf(key, 0, versionConfg.Length);
            if (x > 0)
            {
                textstr = versionConfg.Substring(x + key.Length + 9, 20);
                textstr = textstr.Replace('\"', ' ');
            }
            sr.Close();
            sr.Dispose();
            return textstr;
        }

        public bool Download(string filePath, string fileName, Stream ftpStream, FileStream outputStream)
        {
            int done = 0;
            try
            {
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    try
                    {
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                    }
                    catch
                    {
                        return false;
                    }
                    if (readCount > 0)
                    {
                        if (ftpDownloadUpdate != null)
                        {
                            ftpDownloadUpdate(done += readCount, 0);
                        }
                    }
                }                
            }

            catch (Exception)
            {               
                return false;
            }
            finally {

                if (ftpStream != null) ftpStream.Close();
                if (outputStream != null) outputStream.Close();

            }
            return true;
        }

        public bool zipFile(string TextStorage2, string TextTargetFolder)
        {
            if (string.IsNullOrEmpty(TextStorage2))
            {
                //MessageBox.Show("Storage filename not defined.", "ZipStorer Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
            if (string.IsNullOrEmpty(TextTargetFolder))
            {
                //MessageBox.Show("Target folder not defined.", "ZipStorer Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            try
            {
                // Opens existing zip file
                ZipStorer zip = ZipStorer.Open(TextStorage2, FileAccess.Read);

                // Read all directory contents
                List<ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();
            //    this.listBox2.Items.Clear();

                // Extract all files in target directory
                string path;
                bool result;
                foreach (ZipStorer.ZipFileEntry entry in dir)
                {
                    path = Path.Combine(TextTargetFolder, Path.GetFileName(entry.FilenameInZip));
                    result = zip.ExtractFile(entry, path);
                   
                  //  this.listBox2.Items.Add(path + (result ? "" : " (error)"));
                }
                zip.Close();
                return true;
              
            }
            catch (InvalidDataException)
            {
              
            }
            catch
            {
              
            }
            return false;
        }

        #region İnternet Bağlantisi
        [DllImport("WININET", CharSet = CharSet.Auto)]
        private static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved);
        private enum InternetConnectionState : int
        {
            INTERNET_CONNECTION_OFFLINE = 0x20,
            INTERNET_CONNECTION_CONFIGURED = 0x40
        }

        public static bool IsInternetBaglantisi()
        {
            InternetConnectionState flags = 0;
            bool isConfigured = (flags & InternetConnectionState.INTERNET_CONNECTION_CONFIGURED) != 0;
            bool isConnected = InternetGetConnectedState(ref flags, 0);
            //sonuç
            if (isConnected == true)
            {
                // Console.WriteLine("İnternete Bağlısınız");
                return true;
            }
            else
            {
                //Console.WriteLine("İnternet YOK!");
                return false;
            }
        }
        #endregion
    }
}
